package com.idata.service.services;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.idata.model.dto.KGGraphExploreNodeDTO;
import com.idata.model.dto.KGProxySearchResultDTO;
import com.idata.model.entity.DisputeEntity;
import com.idata.model.entity.DisputeEntity.People;
import com.idata.model.enums.AssociationTypeEnum;
import com.idata.model.enums.DisputeSourceEnum;
import com.idata.model.enums.GraphAnalysisNodeTypeEnum;
import com.idata.model.interfaces.HasDisputeContent;
import com.idata.model.interfaces.HasDisputeTimeSortField;
import com.idata.model.interfaces.HasDisputeType;
import com.idata.model.proxyDO.KGBaseDisputeInfoDO;
import com.idata.model.proxyDO.KGGraphExploreDO;
import com.idata.model.proxyDO.KGResponseDO;
import com.idata.model.request.FilterDisputeConditionalRequest;
import com.idata.model.response.PageResult;
import com.idata.model.response.SelectValue;
import com.idata.model.response.GraphAnalysisSelectConditionResponse;
import com.idata.model.response.GraphExploreResponse;
import com.idata.model.response.GraphExploreResponse.Edge;
import com.idata.model.response.PersonDisputeResponse;
import com.idata.model.utils.DefaultImgUrlManager;
import com.idata.service.config.ProxyUrlProperties;
import com.idata.service.utils.SortDisputeInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @File GraphAnalysisService.Java
 * @Modify Time      @Author    @Version    @Description
 * ------------      -------    --------    -----------
 * 2023/10/10         yd.j      1.0         None
 */
@Slf4j
@Service
public class GraphAnalysisService {

    @Autowired
    private DisputeService disputeService;

    @Autowired
    private ProxyUrlProperties proxyUrlProperties;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private CitizenInfoService citizenInfoService;

    /**
     * 获取下拉框的所有值
     * @param idCard  身份证号码
     * @param request 下拉获取对象
     * @return Map《下拉值，案件ID 》
     */
    public List<String> selectDisputeIds(String idCard, FilterDisputeConditionalRequest request) {
        log.info("FilterDisputeConditionalRequest is " + JSONObject.toJSONString(request));
        DisputeEntity disputeEntity = disputeService.getGraphAtlas(idCard);
        // 先获取所有纠纷id，然后一层一层过滤
        List<String> disputeIdList = disputeEntity.getDisputeIdList();
        // 按照关联方式过滤
        if (CollectionUtil.isNotEmpty(request.getAssociationTypes())) {
            List<String> disputeIds = request.getAssociationTypes().stream()
                    .map(type -> disputeEntity.getAssociatedTypeMap().getOrDefault(type, new ArrayList<>()))
                    .flatMap(List::stream).collect(Collectors.toList());
            disputeIdList = ListUtils.intersection(disputeIdList, disputeIds);
        }
        // 按照纠纷来源
        Integer disputeSource = request.getDisputeSource();
        DisputeSourceEnum disputeSourceEnum = DisputeSourceEnum.fromCode(disputeSource);
        if (disputeSource != null && disputeSourceEnum == null) {
            String errorMsg = "disputeSource 入参错误 : disputeSource = " + disputeSource;
            log.error(errorMsg);
            throw new RuntimeException(errorMsg);
        }
        if (disputeSource != null) {
            disputeIdList = ListUtils.intersection(disputeIdList,
                    disputeEntity.getDisputeSourceMap().getOrDefault(disputeSourceEnum.getCode(), new ArrayList<>()));
        }
        // 按照纠纷编号关联
        if (StringUtils.isNotEmpty(request.getDisputeId())) {
            disputeIdList = disputeIdList.stream().filter(disputeId -> disputeId.equals(request.getDisputeId())).collect(Collectors.toList());
        }
        // 按照身份证号关联
        if (StringUtils.isNotEmpty(request.getDisputePersonIdCard())) {
            Map<People, List<String>> peopleDisputeMap = disputeEntity.getPeopleDisputeMap();
            List<String> disputeIds = peopleDisputeMap.entrySet().stream().filter(entry -> entry.getKey().getIdCard().equals(request.getDisputePersonIdCard()))
                    .map(Entry::getValue).flatMap(List::stream).collect(Collectors.toList());
            disputeIdList = ListUtils.intersection(disputeIdList, disputeIds);
        }
        // 按照纠纷对象关联
        if (CollectionUtil.isNotEmpty(request.getDisputePersonNames())) {
            Map<People, List<String>> peopleDisputeMap = disputeEntity.getPeopleDisputeMap();
            List<String> disputeIds = peopleDisputeMap.entrySet().stream().filter(entry -> request.getDisputePersonNames().contains(entry.getKey().getName()))
                    .map(Entry::getValue).flatMap(List::stream).collect(Collectors.toList());
            disputeIdList = ListUtils.intersection(disputeIdList, disputeIds);
        }
        Map<String, Object> disputeInfoMap = disputeService.obtainDisputeInfoMap(disputeIdList);
        // 纠纷类型
        if (StringUtils.isNotEmpty(request.getDisputeType())) {
            List<String> disputeIds = disputeInfoMap.entrySet().stream().filter(entry -> {
                if (!(entry.getValue() instanceof HasDisputeType)) {
                    return false;
                }
                String disputeType = ((HasDisputeType) entry.getValue()).getDisputeType();
                return StringUtils.equals(request.getDisputeType(), disputeType);
            }).map(Entry::getKey).collect(Collectors.toList());
            disputeIdList = ListUtils.intersection(disputeIdList, disputeIds);
        }
        // 纠纷发生时间
        if (StringUtils.isNotEmpty(request.getDisputeOccurrenceTime())) {
            List<String> disputeIds = disputeInfoMap.entrySet().stream().filter(entry -> {
                if (!(entry.getValue() instanceof HasDisputeTimeSortField)) {
                    return false;
                }
                Date requestOccurrenceTime = DateUtil.parse(request.getDisputeOccurrenceTime(), "yyyy-MM-dd HH:mm:ss");
                Date newRequestTime = new Date(requestOccurrenceTime.getTime() + 100);
                Date disputeTime = ((HasDisputeTimeSortField) entry.getValue()).getDisputeTimeForSort();
                return DateUtil.isSameDay(newRequestTime, disputeTime);
            }).map(Entry::getKey).collect(Collectors.toList());
            disputeIdList = ListUtils.intersection(disputeIdList, disputeIds);
        }
        log.info("多条件过滤后的 disputeIdList: " + JSONObject.toJSONString(disputeIdList));
        return disputeIdList;
    }

    public List<Object> getPersonnelToDisputeAtlas(FilterDisputeConditionalRequest request) {
        String idCard = request.getIdCard();
        DisputeEntity disputeEntity = disputeService.getGraphAtlas(idCard);
        List<String> disputeIds = this.selectDisputeIds(idCard, request);
        disputeIds = ListUtils.intersection(disputeEntity.getDisputeIdList(), disputeIds);
        List<Object> disputeInfoList = disputeIds.stream().map(disputeService::obtainDisputeInfo).collect(Collectors.toList());
        return SortDisputeInfoUtil.sortDisputeInfo(disputeInfoList);
    }

    public List<PersonDisputeResponse> getPersonnelToPeopleAtlas(FilterDisputeConditionalRequest request) {
        String idCard = request.getIdCard();
        DisputeEntity disputeEntity = disputeService.getGraphAtlas(idCard);
        List<String> disputeIds = this.selectDisputeIds(idCard, request);
        Map<People, List<String>> peopleListMap = disputeEntity.getPeopleDisputeMap().entrySet().stream().peek(entry -> {
            entry.setValue(ListUtils.intersection(disputeIds, entry.getValue()));
        }).filter(entry -> CollectionUtil.isNotEmpty(entry.getValue())).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
        List<String> peopleIdCards = peopleListMap.keySet().stream().map(People::getIdCard).collect(Collectors.toList());
        Map<String, List<String>> relationTagMap = citizenInfoService.proxyGetRelationTagMap(idCard, peopleIdCards);
        List<PersonDisputeResponse> resultList = peopleListMap.entrySet().stream()
                .map(entry -> new PersonDisputeResponse(entry.getKey(),
                        SortDisputeInfoUtil.sortDisputeInfo(
                                entry.getValue().stream().map(disputeService::obtainDisputeInfo).collect(Collectors.toList())),
                        relationTagMap.get(entry.getKey().getIdCard())))
                .collect(Collectors.toList());
        List<PersonDisputeResponse> response = resultList.stream().sorted((o1, o2) -> o2.getDisputeInfoList().size() - o1.getDisputeInfoList().size()).collect(Collectors.toList());
        response = response.stream().filter(personDispute -> {
                    if (CollectionUtil.isEmpty(request.getDisputePersonNames())) {
                        return true;
                    }
                    String disputePersonName = Optional.ofNullable(personDispute.getDisputePeople()).map(People::getName).orElse("");
                    return request.getDisputePersonNames().contains(disputePersonName);
                })
                .filter(personDispute -> {
                    if (StringUtils.isEmpty(request.getDisputePersonIdCard())) {
                        return true;
                    }
                    String disputePersonIdCard = Optional.ofNullable(personDispute.getDisputePeople()).map(People::getIdCard).orElse("");
                    return request.getDisputePersonIdCard().equals(disputePersonIdCard);
                }).collect(Collectors.toList());
        return response;
    }

    public GraphExploreResponse<KGGraphExploreNodeDTO> getExploreGraphPP(String idCard) {
        List<KGGraphExploreDO> originalData = getKgGraphExplorePP(idCard);

        GraphExploreResponse<KGGraphExploreNodeDTO> response = new GraphExploreResponse<>();

        originalData = Optional.ofNullable(originalData).orElse(new ArrayList<>()).stream().filter(KGGraphExploreDO::isHasDispute).collect(Collectors.toList());
        Map<String, List<KGGraphExploreDO>> groupByIdCard = originalData.stream().collect(Collectors.groupingBy(KGGraphExploreDO::getId));
        groupByIdCard.forEach((key, list) -> {
            if (CollectionUtil.isEmpty(list)) {
                return;
            }
            KGGraphExploreDO firstNode = list.get(0);
            KGGraphExploreNodeDTO kgGraphExploreNodeDTO = new KGGraphExploreNodeDTO();
            BeanUtils.copyProperties(firstNode, kgGraphExploreNodeDTO);
            response.getNodes().add(kgGraphExploreNodeDTO);
        });
        List<Edge> edgeList = originalData.stream().map(exploreDO -> new Edge(exploreDO.getSrc(), exploreDO.getDst())).collect(Collectors.toList());
        response.setEdges(edgeList);
        fillTypeGraphExploreForResponse(response, GraphAnalysisNodeTypeEnum.PEOPLE);
        return response;
    }

    private List<KGGraphExploreDO> getKgGraphExplorePP(String idCard) {
        List<KGGraphExploreDO> originalData = getKgGraphExplore("/maotiao_backend/search/getExploreGraph", idCard);
        return originalData;
    }

    public GraphAnalysisSelectConditionResponse getSelectCondition(String IDCard) {
        DisputeEntity disputeEntity = disputeService.getGraphAtlas(IDCard);
        GraphAnalysisSelectConditionResponse response = new GraphAnalysisSelectConditionResponse();
        response.setAssociationTypes(disputeEntity.getAssociatedTypeMap().keySet().stream()
                .map(AssociationTypeEnum::fromCode)
                .filter(Objects::nonNull)
                .map(typeEnum -> new SelectValue(typeEnum.getDesc(), String.valueOf(typeEnum.getCode())))
                .collect(Collectors.toList()));
        Set<People> people = disputeEntity.getPeopleDisputeMap().keySet();
        response.setDisputePersonNames(new ArrayList<>(people.stream().map(People::getName).map(SelectValue::new).collect(Collectors.toList())));
        response.setDisputePersonIds(new ArrayList<>(people.stream().map(People::getIdCard).map(SelectValue::new).collect(Collectors.toList())));
        response.setDisputeSources(disputeEntity.getDisputeSourceMap().keySet().stream()
                .map(DisputeSourceEnum::fromCode)
                .filter(Objects::nonNull)
                .filter(disputeSourceEnum -> !disputeSourceEnum.equals(DisputeSourceEnum.HIGH_RISK))
                .map(disputeSourceEnum -> new SelectValue(disputeSourceEnum.getDesc(), String.valueOf(disputeSourceEnum.getCode())))
                .collect(Collectors.toList()));
        response.setDisputeIds(disputeEntity.getDisputeIdList().stream().map(SelectValue::new).collect(Collectors.toList()));
        List<Object> disputeInfoList = disputeService.obtainDisputeInfoList(disputeEntity.getDisputeIdList());
        response.setOccurrenceTime(this.getSelectorTimeList(disputeService.obtainDisputeTime(disputeInfoList)).stream().map(Date::toString).map(SelectValue::new).collect(Collectors.toList()));
        response.setDisputeTypes(disputeService.obtainDisputeType(disputeInfoList).stream().filter(StrUtil::isNotEmpty).distinct().map(SelectValue::new).collect(Collectors.toList()));
        return response;
    }

    private List<Date> getSelectorTimeList(List<Date> dateList) {
        return dateList.stream().map(DateUtil::beginOfDay).distinct().collect(Collectors.toList());
    }

    public GraphExploreResponse<KGGraphExploreNodeDTO> getExploreGraphCP(String disputeId) {
        List<KGGraphExploreDO> originalData = Optional.ofNullable(getkgExploreGraphCP(disputeId)).orElse(new ArrayList<>());
        GraphExploreResponse<KGGraphExploreNodeDTO> response = new GraphExploreResponse<>();
        Map<String, List<KGGraphExploreDO>> groupByIdCard = originalData.stream().collect(Collectors.groupingBy(KGGraphExploreDO::getId));
        groupByIdCard.forEach((key, list) -> {
            if (CollectionUtil.isEmpty(list)) {
                return;
            }
            KGGraphExploreDO firstNode = list.get(0);
            KGGraphExploreNodeDTO kgGraphExploreNodeDTO = new KGGraphExploreNodeDTO();
            BeanUtils.copyProperties(firstNode, kgGraphExploreNodeDTO);
            response.getNodes().add(kgGraphExploreNodeDTO);
        });
        List<Edge> edgeList = originalData.stream().map(exploreDO -> new Edge(disputeId, exploreDO.getId())).collect(Collectors.toList());
        response.setEdges(edgeList);
        fillTypeGraphExploreForResponse(response, GraphAnalysisNodeTypeEnum.PEOPLE);
        return response;
    }

    private List<KGGraphExploreDO> getkgExploreGraphCP(String disputeId) {
        List<KGGraphExploreDO> originalData = getKgGraphExplore("/maotiao_backend/search/getThingExploreGraph", disputeId);
        // 当传入id为idCard时，该人事接口返回的body会和DO无法映射，导致id为空，该情况属于异常情况，pc接口调用的是图谱分析的缓存相关接口
        originalData = originalData.stream().filter(DO -> StringUtils.isNotEmpty(DO.getId())).collect(Collectors.toList());
        return originalData;
    }

    private List<KGGraphExploreDO> getKgGraphExplore(String path, String disputeIdOrIdCard) {
        String url = this.proxyUrlProperties.getProxyUrl().getKg() + path + "?searchValue=" + disputeIdOrIdCard;
        ParameterizedTypeReference<KGResponseDO<List<KGGraphExploreDO>>> typeReference = new ParameterizedTypeReference<KGResponseDO<List<KGGraphExploreDO>>>() {
        };
        ResponseEntity<KGResponseDO<List<KGGraphExploreDO>>> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, typeReference);

        List<KGGraphExploreDO> originalData = responseEntity.getBody().getData();
        return originalData;
    }


    public GraphExploreResponse<KGGraphExploreNodeDTO> getExploreGraphPC(String idCard) {
        // 获取所有纠纷信息
        DisputeEntity disputeEntity = disputeService.getGraphAtlas(idCard);
        List<Object> disputeInfoList = Optional.ofNullable(disputeService.obtainDisputeInfoList(disputeEntity.getDisputeIdList())).orElse(new ArrayList<>());
        Map<String, List<String>> associationNamesMap = new HashMap<>();
        disputeInfoList.forEach(disputeInfo -> {
            if(!(disputeInfo instanceof KGBaseDisputeInfoDO)) {
                return;
            }
            String disputeId = ((KGBaseDisputeInfoDO) disputeInfo).getDisputeId();
            List<String> associationNameList = ((KGBaseDisputeInfoDO) disputeInfo).getAssociationNameList();
            associationNamesMap.put(disputeId, associationNameList);
        });

        GraphExploreResponse<KGGraphExploreNodeDTO> response = new GraphExploreResponse<>();
        List<KGGraphExploreNodeDTO> nodes = new ArrayList<>();
        for (Object disputeInfo : disputeInfoList) {
            KGGraphExploreNodeDTO node = new KGGraphExploreNodeDTO();
            if (!(disputeInfo instanceof KGBaseDisputeInfoDO)) {
                continue;
            }
            if (!(disputeInfo instanceof HasDisputeContent)) {
                return null;
            }
            String disputeId = ((KGBaseDisputeInfoDO) disputeInfo).getDisputeId();
            node.setId(disputeId);
            node.setContent(((HasDisputeContent) disputeInfo).getDisputeContent());
            node.setAvatarUrl(DefaultImgUrlManager.DEFAULT_DISPUTE_IMG_URL);
            node.setRelationList(associationNamesMap.get(disputeId));
            nodes.add(node);
        }
        List<Edge> edges = nodes.stream().map(node -> new Edge(node.getId(), idCard)).collect(Collectors.toList());
        response.setNodes(nodes);
        response.setEdges(edges);
        fillTypeGraphExploreForResponse(response, GraphAnalysisNodeTypeEnum.DISPUTE);
        return response;
    }

    // 根据人获取所有事
    public GraphExploreResponse<KGGraphExploreNodeDTO> getExploreGraphPCWithCondition(FilterDisputeConditionalRequest request) {
        // 获取所有纠纷信息
        DisputeEntity disputeEntity = disputeService.getGraphAtlas(request.getIdCard());
        List<Object> disputeInfoList = Optional.ofNullable(disputeService.obtainDisputeInfoList(disputeEntity.getDisputeIdList())).orElse(new ArrayList<>());
        Map<String, List<String>> associationNamesMap = new HashMap<>();
        disputeInfoList.forEach(disputeInfo -> {
            if(!(disputeInfo instanceof KGBaseDisputeInfoDO)) {
                return;
            }
            String disputeId = ((KGBaseDisputeInfoDO) disputeInfo).getDisputeId();
            List<String> associationNameList = ((KGBaseDisputeInfoDO) disputeInfo).getAssociationNameList();
            associationNamesMap.put(disputeId, associationNameList);
        });

        // 获取所有探索信息
        List<Object> personnelToDisputeAtlas = this.getPersonnelToDisputeAtlas(request);
        List<KGGraphExploreNodeDTO> nodes = personnelToDisputeAtlas.stream().map(disputeInfo -> {
            if (!(disputeInfo instanceof KGBaseDisputeInfoDO)) {
                return null;
            }
            if (!(disputeInfo instanceof HasDisputeContent)) {
                return null;
            }
            KGGraphExploreNodeDTO kgGraphExploreNodeDTO = new KGGraphExploreNodeDTO();
            String disputeId = ((KGBaseDisputeInfoDO) disputeInfo).getDisputeId();
            kgGraphExploreNodeDTO.setId(disputeId);
            kgGraphExploreNodeDTO.setContent(((HasDisputeContent) disputeInfo).getDisputeContent());
            kgGraphExploreNodeDTO.setAvatarUrl(DefaultImgUrlManager.DEFAULT_DISPUTE_IMG_URL);
            kgGraphExploreNodeDTO.setRelationList(associationNamesMap.get(disputeId));
            return kgGraphExploreNodeDTO;
        }).collect(Collectors.toList());
        KGGraphExploreNodeDTO graphExploreParty = this.getGraphExploreParty(request.getIdCard());
        if (graphExploreParty == null) {
            return new GraphExploreResponse<>();
        }

        List<Edge> edges = nodes.stream().map(node -> new Edge(node.getId(), request.getIdCard())).collect(Collectors.toList());
        // 探索信息加上当事人信息
        nodes.add(graphExploreParty);
        GraphExploreResponse<KGGraphExploreNodeDTO> response = new GraphExploreResponse<KGGraphExploreNodeDTO>().setNodes(nodes).setEdges(edges);
        fillTypeGraphExploreForResponse(response, GraphAnalysisNodeTypeEnum.DISPUTE);
        return response;
    }

    public GraphExploreResponse<KGGraphExploreNodeDTO> getExploreGraphPPWithCondition(FilterDisputeConditionalRequest request) {
        List<PersonDisputeResponse> personnelToPeopleAtlas = this.getPersonnelToPeopleAtlas(request);
        List<People> shouldShowPeople = personnelToPeopleAtlas.stream().map(PersonDisputeResponse::getDisputePeople).collect(Collectors.toList());
        List<KGGraphExploreDO> allExplorePeopleInfo = Optional.ofNullable(this.getKgGraphExplorePP(request.getIdCard())).orElse(new ArrayList<>());
        Map<String, KGGraphExploreDO> idCardAndInfoMap = allExplorePeopleInfo.stream().collect(Collectors.toMap(KGGraphExploreDO::getId, Function.identity(), (oldValue, newValue) -> newValue));
        Set<String> shouldShowPeopleId = shouldShowPeople.stream().map(People::getIdCard).collect(Collectors.toSet());
        List<KGGraphExploreDO> shouldShowPeopleInfo = shouldShowPeopleId.stream().map(idCardAndInfoMap::get).filter(Objects::nonNull).collect(Collectors.toList());
        List<KGGraphExploreNodeDTO> nodes = shouldShowPeopleInfo.stream().map(peopleInfoDo -> {
            KGGraphExploreNodeDTO kgGraphExploreNodeDTO = new KGGraphExploreNodeDTO();
            BeanUtils.copyProperties(peopleInfoDo, kgGraphExploreNodeDTO);
            return kgGraphExploreNodeDTO;
        }).collect(Collectors.toList());
        KGGraphExploreNodeDTO graphExploreParty = this.getGraphExploreParty(request.getIdCard());
        if (graphExploreParty == null) {
            return new GraphExploreResponse<>();
        }
        nodes.add(graphExploreParty);
        HashSet<String> shouldShowPeopleIdAndPartyId = new HashSet<>(shouldShowPeopleId);
        shouldShowPeopleIdAndPartyId.add(graphExploreParty.getId());

        ArrayList<KGGraphExploreDO> newAllExplorePeopleInfo = new ArrayList<>();
        Map<String, List<KGGraphExploreDO>> groupByIdCard = allExplorePeopleInfo.stream().collect(Collectors.groupingBy(KGGraphExploreDO::getId));
        groupByIdCard.forEach((key, list) -> {
            if (CollectionUtil.isEmpty(list)) {
                return;
            }
            KGGraphExploreDO firstNode = list.get(0);
            newAllExplorePeopleInfo.add(firstNode);
        });

        List<Edge> edges = newAllExplorePeopleInfo.stream().filter(explorePeopleInfo -> shouldShowPeopleIdAndPartyId.contains(explorePeopleInfo.getSrc()) && shouldShowPeopleIdAndPartyId.contains(explorePeopleInfo.getDst()))
                .map(explorePeopleInfo -> new Edge(explorePeopleInfo.getSrc(), explorePeopleInfo.getDst())).collect(Collectors.toList());
        GraphExploreResponse<KGGraphExploreNodeDTO> response = new GraphExploreResponse<KGGraphExploreNodeDTO>().setNodes(nodes).setEdges(edges);
        fillTypeGraphExploreForResponse(response, GraphAnalysisNodeTypeEnum.PEOPLE);
        return response;
    }

    private KGGraphExploreNodeDTO getGraphExploreParty(String idCard) {
        // 搜索到数据
        PageResult<KGProxySearchResultDTO> kgCitizenResponse = citizenInfoService.proxySearchCitizen(
                idCard, 0, 1);
        if (kgCitizenResponse.getTotal() == null
                || kgCitizenResponse.getTotal().equals(0L)
                || CollectionUtil.isEmpty(kgCitizenResponse.getList())) {
            return null;
        }

        KGProxySearchResultDTO kgProxySearchResultDTO = kgCitizenResponse.getList().get(0);
        return new KGGraphExploreNodeDTO().setContent(kgProxySearchResultDTO.getKgCitizenDO().getPeople().getName())
                .setAvatarUrl(kgProxySearchResultDTO.getKgCitizenDO().getPeople().getAvatarUrl())
                .setId(kgProxySearchResultDTO.getKgCitizenDO().getPeople().getIdCard());
    }

    private void fillTypeGraphExploreForResponse(GraphExploreResponse<KGGraphExploreNodeDTO> response,
                                                 GraphAnalysisNodeTypeEnum graphAnalysisNodeTypeEnum) {
        List<KGGraphExploreNodeDTO> nodes = response.getNodes();
        if (CollectionUtil.isEmpty(nodes)) {
            return;
        }
        nodes.forEach(node -> node.setType(graphAnalysisNodeTypeEnum.getCode()));
    }
}
